package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Dictionary;
import com.company.project.service.DictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/dictionary")
public class    DictionaryController {
    @Resource
    private DictionaryService dictionaryService;
    /**
     * 查询期刊周期
     * @return
     */
    @RequestMapping(value = "/List",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,List<Dictionary>>selectDic(){
        return dictionaryService.getDic();
    }

    /**
     * 后台查询期刊
     * @return
     */
    @GetMapping("/adminDictionary/{index}")
    @ResponseBody
    public Map<String,Object> adminSelectDic(@PathVariable Integer index){
        return dictionaryService.adminSelectDic(index);
    }

    /**
     *后台添加期刊类型
     * @param dictionary
     * @return
     */
    @PostMapping("/adminAddDic")
    @ResponseBody
    public Boolean adminAddDic(@RequestBody Dictionary dictionary){
        return dictionaryService.adminAddDic(dictionary);
    }

    /**
     * 根据id查询字典详情
     * @param id
     * @return
     */
    @GetMapping("/DMParticular/{id}")
    @ResponseBody
    public Result DMParticular(@PathVariable Integer id){
        List<Dictionary> dictionaries = dictionaryService.DMParticular(id);
        return ResultGenerator.genSuccessResult(dictionaries);
    }
    /**
     * 后台修改期刊类型
     * @param dictionary
     * @return
     */
    @PostMapping("/adminUpDic")
    @ResponseBody
    public Boolean adminUpDic(@RequestBody Dictionary dictionary){
        return dictionaryService.adminUpDic(dictionary);
    }

    /**
     * 后台删除期刊字典
     * @param id
     * @return
     */
    @GetMapping("/adminDelDic/{id}")
    @ResponseBody
    public Boolean adminDelDic(@PathVariable Integer id){
        return dictionaryService.adminDelDic(id);
    }

    @PostMapping("/add")
    public Result add(Dictionary dictionary) {
        dictionaryService.save(dictionary);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        dictionaryService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Dictionary dictionary) {
        dictionaryService.update(dictionary);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Dictionary dictionary = dictionaryService.findById(id);
        return ResultGenerator.genSuccessResult(dictionary);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Dictionary> list = dictionaryService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    /**
     * 后台查询所有类型
     */
    @GetMapping("/AllType")
    @ResponseBody
    public Result selectType(){
        List<Dictionary> dictionaries = dictionaryService.selectType();
        return ResultGenerator.genSuccessResult(dictionaries);
    }

    /**
     * 查询期刊等级
     * @return
     */
    @GetMapping("/AdminRank")
    @ResponseBody
    public Result AdminRank(){
        List<Dictionary> dictionaries = dictionaryService.AdminRank();
        return ResultGenerator.genSuccessResult(dictionaries);
    }
    /**
     * 查询发刊周期
     */
    @GetMapping("/AdminPeriod")
    @ResponseBody
    public Result AdminPeriod(){
        List<Dictionary> dictionaries = dictionaryService.AdminPeriod();
        return ResultGenerator.genSuccessResult(dictionaries);
    }
}
